Lab Help

In case you face an error while going through all the steps, the final code for app.py is being shared here as a ready reference. Please note that this should be used only as a last resort to ensure that you gain the learning intended through this lab.

Final code for app.py
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  1. # Import necessary libraries from Flask
  2. from flask import Flask, redirect, request, render_template, url_for
  3. # Instantiate Flask application
  4. app = Flask(__name__)
  5. # Sample data representing transactions
  6. transactions = [
  7. {'id': 1, 'date': '2023-06-01', 'amount': 100},
  8. {'id': 2, 'date': '2023-06-02', 'amount': -200},
  9. {'id': 3, 'date': '2023-06-03', 'amount': 300}
  10. ]
  11. # Read operation: Route to list all transactions
  12. @app.route("/")
  13. def get_transactions():
  14. # Render the transactions list template and pass the transactions data
  15. return render_template("transactions.html", transactions=transactions)
  16. # Create operation: Route to display and process add transaction form
  17. @app.route("/add", methods=["GET", "POST"])
  18. def add_transaction():
  19. if request.method == 'POST':
  20. # Extract form data to create a new transaction object
  21. transaction = {
  22. 'id': len(transactions) + 1, # Generate a new ID based on the current length of the transactions list
  23. 'date': request.form['date'], # Get the 'date' field value from the form
  24. 'amount': float(request.form['amount']) # Get the 'amount' field value from the form and convert it to a float
  25. }
  26. # Append the new transaction to the transactions list
  27. transactions.append(transaction)
  28. # Redirect to the transactions list page after adding the new transaction
  29. return redirect(url_for("get_transactions"))
  30. # Render the form template to display the add transaction form if the request method is GET
  31. return render_template("form.html")
  32. # Update operation: Route to display and process edit transaction form
  33. @app.route("/edit/<int:transaction_id>", methods=["GET", "POST"])
  34. def edit_transaction(transaction_id):
  35. if request.method == 'POST':
  36. # Extract the updated values from the form fields
  37. date = request.form['date']
  38. amount = float(request.form['amount'])
  39. # Find the transaction with the matching ID and update its values
  40. for transaction in transactions:
  41. if transaction['id'] == transaction_id:
  42. transaction['date'] = date # Update the 'date' field of the transaction
  43. transaction['amount'] = amount # Update the 'amount' field of the transaction
  44. break # Exit the loop once the transaction is found and updated
  45. # Redirect to the transactions list page after updating the transaction
  46. return redirect(url_for("get_transactions"))
  47. # Find the transaction with the matching ID and render the edit form if the request method is GET
  48. for transaction in transactions:
  49. if transaction['id'] == transaction_id:
  50. # Render the edit form template and pass the transaction to be edited
  51. return render_template("edit.html", transaction=transaction)
  52. # Delete operation: Route to delete a transaction
  53. @app.route("/delete/<int:transaction_id>")
  54. def delete_transaction(transaction_id):
  55. # Find the transaction with the matching ID and remove it from the list
  56. for transaction in transactions:
  57. if transaction['id'] == transaction_id:
  58. transactions.remove(transaction) # Remove the transaction from the transactions list
  59. break # Exit the loop once the transaction is found and removed
  60. # Redirect to the transactions list page after deleting the transaction
  61. return redirect(url_for("get_transactions"))
  62. # Run the Flask application
  63. if __name__ == "__main__":
  64. app.run(debug=True)